module.exports = 
{
   createReader: function(lineCallback)
   {
      this.buffer = '';
      this.resultQueue = new Array();
      this.lineCallback = lineCallback;
      return this;
   },
   append: function(chunk)
   {
      this.buffer += chunk;
      var i = this.buffer.lastIndexOf('\n');
      if (i != -1)
      {
         Array.prototype.push.apply(this.resultQueue, this.buffer.substr(0, i).split('\n'));
         this.buffer = this.buffer.substr(i + 1);
         if (this.lineCallback != null)
         {
            this.dispatch(this.resultQueue, this.lineCallback);
         }
      }
   },
   dispatch: function(queue, callback)
   {
      while (queue.length > 0)
      {
         callback(queue.shift());
      }
   },
   close : function()
   {
      if (this.buffer.length > 0)
      {
         this.resultQueue.push(this.buffer);
         buffer = '';
         if (this.lineCallback != null)
         {
            dispatch(this.resultQueue, this.lineCallback);
         }
      }
   }
}